// 认证相关的 API 调用
const auth = {
    // 登录
    async login(username, password) {
        const formData = new URLSearchParams();
        formData.append('username', username);
        formData.append('password', password);

        const response = await window.apiClient.post('/auth/login', formData, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        });
        return response.data;
    },

    // 获取当前用户信息
    async getCurrentUser() {
        const response = await window.apiClient.get('/auth/me');
        return response.data;
    },

    // 登出
    logout() {
        localStorage.removeItem(window.API_CONFIG.TOKEN_KEY);
        localStorage.removeItem(window.API_CONFIG.USER_KEY);
        window.location.href = '/index.html';
    },

    // 检查是否已登录
    isAuthenticated() {
        return !!localStorage.getItem(window.API_CONFIG.TOKEN_KEY);
    },

    // 获取存储的用户信息
    getStoredUser() {
        const userStr = localStorage.getItem(window.API_CONFIG.USER_KEY);
        return userStr ? JSON.parse(userStr) : null;
    }
}; 